//
//  LazyCartViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/7.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "LazyCartViewController.h"
#import "MyHeaderView.h"
#import "LastTableViewCell.h"
#import "BluetoothSetVC.h"
#import "myWebViewController.h"
#import "SearchBlueViewController.h"
#import "SoundChangeViewController.h"

@interface LazyCartViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView *_tableView;
    UIView *popView;
    UIImageView *barCodeImg;
    UIView *headView;
    UIImageView *headImg;
    UILabel *userLabel;
    UILabel *shopLabel;
    UILabel *addressLabel;
    UIView *footView;
    NSString *updateURL;
}
@end

@implementation LazyCartViewController

-(UIStatusBarStyle)preferredStatusBarStyle {//修改状态栏的颜色
    return UIStatusBarStyleLightContent;
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([[ShopCommonModel share].shopname isEqualToString:@""]) {
        [headImg sd_setImageWithURL:[NSURL URLWithString:[ShopCommonModel share].shoplogo] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
        userLabel.text = @"";
        shopLabel.text = @"";
        addressLabel.text = [NSString stringWithFormat:@"   商铺地址:%@",@""];
    }else {
        [headImg sd_setImageWithURL:[NSURL URLWithString:[ShopCommonModel share].shoplogo] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
        userLabel.text = [AppData share].username;
        shopLabel.text = [ShopCommonModel share].shopname;
        addressLabel.text = [NSString stringWithFormat:@"   商铺地址:%@",[ShopCommonModel share].address];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackColor;
    [self initNav:@"我的" doBack:NO];
    [self creataHeaderV];
    [self creataFooterV];
    [self createTable];//创建表视图
}
-(void)creataHeaderV {
    headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 137)];
    headView.backgroundColor = MainColor;
    [self.view addSubview:headView];
    
    headImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 35, 52, 52)];
    [headImg sd_setImageWithURL:[NSURL URLWithString:[[ShopCommonModel share].shoplogo safeString]] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
    headImg.layer.cornerRadius = 26;
    headImg.layer.masksToBounds = YES;
    [headView addSubview:headImg];
    
    shopLabel = [[UILabel alloc]initWithFrame:CGRectMake(77, 38, ScreenWidth-87, 25)];
    shopLabel.text = [ShopCommonModel share].shopname;
    shopLabel.textColor = [UIColor whiteColor];
    shopLabel.font = [UIFont boldSystemFontOfSize:15];
    [headView addSubview:shopLabel];
    
    userLabel = [[UILabel alloc]initWithFrame:CGRectMake(77, 62, ScreenWidth-87, 25)];
    userLabel.text = [AppData share].username;
    userLabel.textColor = [UIColor whiteColor];
    userLabel.font = [UIFont systemFontOfSize:14];
    [headView addSubview:userLabel];
    
    UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(0, 102, ScreenWidth, 35)];
    v1.backgroundColor = SetColor(0, 126, 185, 1);
    [headView addSubview:v1];
    addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-10, 35)];
    addressLabel.text = [NSString stringWithFormat:@"   商铺地址:%@",[ShopCommonModel share].address];
    addressLabel.textColor = SetColor(238, 238, 238, 1);
    addressLabel.font = [UIFont systemFontOfSize:14];
    addressLabel.backgroundColor = [UIColor clearColor];
    [v1 addSubview:addressLabel];
}
-(void)creataFooterV {
    footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 95)];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    
    UIButton *loginOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    loginOutBtn.frame = CGRectMake(10, 25, ScreenWidth-20, 45);
    loginOutBtn.layer.cornerRadius = 3.0;
    loginOutBtn.layer.borderColor = SetColor(221, 221, 221, 1).CGColor;
    loginOutBtn.layer.borderWidth = 1.0;
    loginOutBtn.layer.masksToBounds = YES;
    [loginOutBtn setTitleColor:MainColor forState:UIControlStateNormal];
    loginOutBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loginOutBtn.backgroundColor = [UIColor whiteColor];
    [footView addSubview:loginOutBtn];
    [loginOutBtn addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
}
-(void)quit {//退出登录
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    alert.tag = 2000;
    [alert show];
}
-(void)createTable {//创建表视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 50) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableHeaderView = headView;
    _tableView.tableFooterView = footView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else if (section == 1) {
        return 2;
    }else {
        return 2;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {//头部高度
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {//尾部高度
    return 0.00001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"LastTableViewCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *titArr = @[@[@"认证资料",@"修改密码"],@[@"蓝牙打印机",@"消息提醒"],@[@"版本信息",@"客服热线"]];
    cell.titleLabel.text = titArr[indexPath.section][indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.timeLabel.textAlignment = NSTextAlignmentRight;
        cell.timeLabel.text = @"已认证";
        cell.timeLabel.textColor = SetColor(0, 140, 0, 1);
    }else if (indexPath.section == 2 && indexPath.row == 0) {
        cell.timeLabel.textAlignment = NSTextAlignmentRight;
        cell.timeLabel.text = @"检查更新";
        cell.timeLabel.textColor = SetColor(160, 160, 160, 1);
    }else if (indexPath.section == 2 && indexPath.row == 1) {
        cell.timeLabel.textAlignment = NSTextAlignmentLeft;
        cell.timeLabel.text = [ShopCommonModel share].customerphone;
        cell.timeLabel.frame = CGRectMake(90, 0, 150, 44);
        cell.timeLabel.textColor = SetColor(255, 64, 0, 1);
        cell.jiantouImg.hidden = YES;
        UIImageView *phoneImg = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-35, 10, 25, 25)];
        phoneImg.image = [UIImage imageNamed:@"phone.png"];
        [cell addSubview:phoneImg];
    }else {
        cell.timeLabel.text = @"";
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
//        myWebViewController *myWebVC = [[myWebViewController alloc]init];
//        myWebVC.titleName = @"认证资料";
//        [self.navigationController pushViewController:myWebVC animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1) {
        myWebViewController *myWebVC = [[myWebViewController alloc]init];
        myWebVC.titleName = @"修改密码";
        myWebVC.requestUrl = [NSString stringWithFormat:changePWD,[AppData share].username,[Utils md5SignStr]];
        //NSLog(@"%@",myWebVC.requestUrl);
        [self.navigationController pushViewController:myWebVC animated:YES];
    }
    else if (indexPath.section == 1 && indexPath.row == 0) {
        SearchBlueViewController *BVC = [[SearchBlueViewController alloc]init];
        [self.navigationController pushViewController:BVC animated:YES];
    }
    else if (indexPath.section == 1 && indexPath.row == 1) {//声音设置
        SoundChangeViewController *SVC = [[SoundChangeViewController alloc]init];
        [self.navigationController pushViewController:SVC animated:YES];
    }else if (indexPath.section == 2 && indexPath.row == 0) {
        [self getVersionMessg];
    }else {
        if ([ShopCommonModel share].customerphone.length == 0 || [ShopCommonModel share].customerphone == nil) {
            
        }else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:[ShopCommonModel share].customerphone delegate:self cancelButtonTitle:@"呼叫" otherButtonTitles:@"取消", nil];
            alert.tag = 1000;
            [alert show];
        }
    }
}
#pragma mark - 版本升级
- (void)getVersionMessg {
    NSString *banbenID = @"1.0.0"; // 版本号
    NSString *url = @"";
    if ([banbenID isEqualToString:@"1.0.1"]) { // APPStore版本升级
        NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
        NSString *cid = [userDefaul objectForKey:@"cid"];
        url = [NSString stringWithFormat:Updatestore,banBen,cid];
        [NTNetUtil sendRequestWithUrl:url requestName:UpdateVersion method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *suc = dic[@"r"];
            if ([suc isEqualToString:@"T"]) {
                NSDictionary *ios = dic[@"upgrade"];
                NSString *need = [ios[@"isneedupgrade"] safeString];
                NSString *title = [ios[@"title"] safeString];
                NSString *note = [ios[@"note"] safeString];
                if ([need isEqualToString:@"0"]) {//不强制
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
                    alert.tag = 100;
                    [alert show];
                }else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                    alert.tag = 101;
                    [alert show];
                }
                updateURL = [ios[@"url"] safeString];
            }else {
                [[MessShowView share] showWithMess:@"当前为最新版本"];
            }
        }];
    }else{ // 企业版本号
        NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
        NSString *cid = [userDefaul objectForKey:@"cid"];
        url = [NSString stringWithFormat:UpdateVersion,banBen,cid];
        [NTNetUtil sendRequestWithUrl:url requestName:UpdateVersion method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *suc = dic[@"r"];
            if ([suc isEqualToString:@"T"])
            {
                NSDictionary *ios = dic[@"upgrade"];
                if([[ios allKeys] containsObject:@"key"])
                {//包含upgrade字段，升级
                    NSString *need = [ios[@"isneedupgrade"] safeString];
                    NSString *title = [ios[@"title"] safeString];
                    NSString *note = [ios[@"note"] safeString];
                    if ([need isEqualToString:@"0"])
                    {//不强制
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
                        alert.tag = 100;
                        [alert show];
                    }else {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                        alert.tag = 101;
                        [alert show];
                    }
                }else {
                    [[MessShowView share] showWithMess:@"当前为最新版本"];
                }
                updateURL = [ios[@"url"] safeString];
            }
        }];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1000) {
        if (buttonIndex == 0) {
            NSString *phone = [[ShopCommonModel share].customerphone stringByReplacingOccurrencesOfString:@"-" withString:@""];
            NSLog(@"========%@",phone);
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel://%@",phone];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }else {
            
        }
    }else if (alertView.tag == 2000) {
        if (buttonIndex == 1) {
            
        }else {
            NSString *url = [NSString stringWithFormat:lzgLoginOut,[AppData share].username];
            [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
                NSDictionary *dict = result;
                NSString *r = dict[@"r"];
                if ([r isEqualToString:@"T"]) {
                    [[AppData share] clearValue];
                    [NSUserDefualManager storageValue:@"" forKey:Psw];
                    [NSUserDefualManager storageValue:@"0" forKey:@"doLogin"];
                    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults]; // 蓝牙开关状态
                    [userDefaul setObject:@"off" forKey:@"switch"];
                    LoginViewController *loginVC = [[LoginViewController alloc]init];
                    [self presentViewController:loginVC animated:YES completion:^{
                        
                    }];
                }else {
                    [[MessShowView share] showWithMess:dict[@"m"]];
                }
            }];
        }
    }else if (alertView.tag == 100) {
        if (buttonIndex == 1) { // 更新
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",updateURL]]];
        }
    }else if (alertView.tag == 101) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",updateURL]]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
